﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MTC.Common.Configuration;
using MTC.Common.DTO;

namespace MTC.DataAccess
{
    public static class PlateTypeDataAccess
    {
        public static List<PlateType> GetPlateTypes()
        {
            List<PlateType> listOfPlateType = new List<PlateType>();

            Database db = DatabaseFactory.CreateDatabase(ApplicationSettings.Settings.Database.ConnectionStringName);
            if (db != null)
            {
                const string sqlCommandText = "spPlateType_GetAllPlateTypes";
                using (SqlCommand sqlCommand = db.GetStoredProcCommand(sqlCommandText) as SqlCommand)
                {

                    using (SqlDataReader dataReader = db.ExecuteReader(sqlCommand) as SqlDataReader)
                    {
                        PlateType entity;

                        if (dataReader != null)
                            while (dataReader.Read())
                            {
                                entity = new PlateType();
                                if (!Convert.IsDBNull(dataReader["PlateTypeId"]))
                                    entity.PlateTypeId = Convert.ToInt32(dataReader["PlateTypeId"]);
                                if (!Convert.IsDBNull(dataReader["PlateDescription"]))
                                    entity.PlateDescription = Convert.ToString(dataReader["PlateDescription"]);

                                listOfPlateType.Add(entity);
                            }
                    }
                }
            }
            return listOfPlateType;
            
        }


        public static int Insert(PlateType platetype)
        {
            Database db = DatabaseFactory.CreateDatabase(ApplicationSettings.Settings.Database.ConnectionStringName);

            const string sqlCommand = "spPlateType_Insert";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);

            db.AddInParameter(dbCommand, "@PlateTypeDescription", DbType.String, platetype.PlateDescription);
            return db.ExecuteNonQuery(dbCommand);

        }

        public static bool Update(PlateType platetype)
        {
            Database db = DatabaseFactory.CreateDatabase(ApplicationSettings.Settings.Database.ConnectionStringName);

            const string sqlCommandText = "spPlateType_Update";
            int result;
            using (SqlCommand sqlCommand = db.GetStoredProcCommand(sqlCommandText) as SqlCommand)
            {
                db.AddInParameter(sqlCommand, "@PlateTypeId", DbType.Int32, platetype.PlateTypeId);
                db.AddInParameter(sqlCommand, "@PlateTypeDescription", DbType.String, platetype.PlateDescription);

                result = db.ExecuteNonQuery(sqlCommand);
            }
            return result > 0 ? true : false;
        }

        public static bool Delete(PlateType platetype)
        {
            Database db = DatabaseFactory.CreateDatabase(ApplicationSettings.Settings.Database.ConnectionStringName);

            const string sqlCommandText = "spPlateType_Delete";
            int result;
            using (SqlCommand sqlCommand = db.GetStoredProcCommand(sqlCommandText) as SqlCommand)
            {
                db.AddInParameter(sqlCommand, "@PlateTypeId", DbType.Int32, platetype.PlateTypeId);
                result = db.ExecuteNonQuery(sqlCommand);
            }

            return result > 0 ? true : false;

        }
    
    
    }
}
